home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1PQ36MD (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  4.1 KB  |  147 lines

  1. package java.beans;
  2.  
  3. import java.lang.reflect.Method;
  4.  
  5. public class PropertyDescriptor extends FeatureDescriptor {
  6.    private Class propertyType;
  7.    private Method readMethod;
  8.    private Method writeMethod;
  9.    private boolean bound;
  10.    private boolean constrained;
  11.    private Class propertyEditorClass;
  12.  
  13.    PropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y) {
  14.       super(x, y);
  15.       this.readMethod = x.readMethod;
  16.       this.propertyType = x.propertyType;
  17.       if (y.readMethod != null) {
  18.          this.readMethod = y.readMethod;
  19.       }
  20.  
  21.       this.writeMethod = x.writeMethod;
  22.       if (y.writeMethod != null) {
  23.          this.writeMethod = y.writeMethod;
  24.       }
  25.  
  26.       this.propertyEditorClass = x.propertyEditorClass;
  27.       if (y.propertyEditorClass != null) {
  28.          this.propertyEditorClass = y.propertyEditorClass;
  29.       }
  30.  
  31.       this.bound = x.bound | y.bound;
  32.       this.constrained = x.constrained | y.constrained;
  33.  
  34.       try {
  35.          this.findPropertyType();
  36.       } catch (IntrospectionException var3) {
  37.          throw new Error("PropertyDescriptor: internal error while merging PDs");
  38.       }
  39.    }
  40.  
  41.    public PropertyDescriptor(String propertyName, Class beanClass) throws IntrospectionException {
  42.       ((FeatureDescriptor)this).setName(propertyName);
  43.       String base = this.capitalize(propertyName);
  44.       this.writeMethod = Introspector.findMethod(beanClass, "set" + base, 1);
  45.       if (this.writeMethod.getParameterTypes()[0] == Boolean.TYPE) {
  46.          try {
  47.             this.readMethod = Introspector.findMethod(beanClass, "is" + base, 0);
  48.          } catch (Exception var4) {
  49.          }
  50.       }
  51.  
  52.       if (this.readMethod == null) {
  53.          this.readMethod = Introspector.findMethod(beanClass, "get" + base, 0);
  54.       }
  55.  
  56.       this.findPropertyType();
  57.    }
  58.  
  59.    public PropertyDescriptor(String propertyName, Class beanClass, String getterName, String setterName) throws IntrospectionException {
  60.       ((FeatureDescriptor)this).setName(propertyName);
  61.       this.readMethod = Introspector.findMethod(beanClass, getterName, 0);
  62.       this.writeMethod = Introspector.findMethod(beanClass, setterName, 1);
  63.       this.findPropertyType();
  64.    }
  65.  
  66.    public PropertyDescriptor(String propertyName, Method getter, Method setter) throws IntrospectionException {
  67.       ((FeatureDescriptor)this).setName(propertyName);
  68.       this.readMethod = getter;
  69.       this.writeMethod = setter;
  70.       this.findPropertyType();
  71.    }
  72.  
  73.    private String capitalize(String s) {
  74.       char[] chars = s.toCharArray();
  75.       chars[0] = Character.toUpperCase(chars[0]);
  76.       return new String(chars);
  77.    }
  78.  
  79.    private void findPropertyType() throws IntrospectionException {
  80.       try {
  81.          this.propertyType = null;
  82.          if (this.readMethod != null) {
  83.             if (this.readMethod.getParameterTypes().length != 0) {
  84.                throw new IntrospectionException("bad read method arg count");
  85.             }
  86.  
  87.             this.propertyType = this.readMethod.getReturnType();
  88.             if (this.propertyType == Void.TYPE) {
  89.                throw new IntrospectionException("read method " + this.readMethod.getName() + " returns void");
  90.             }
  91.          }
  92.  
  93.          if (this.writeMethod != null) {
  94.             Class[] params = this.writeMethod.getParameterTypes();
  95.             if (params.length != 1) {
  96.                throw new IntrospectionException("bad write method arg count");
  97.             }
  98.  
  99.             if (this.propertyType != null && this.propertyType != params[0]) {
  100.                throw new IntrospectionException("type mismatch between read and write methods");
  101.             }
  102.  
  103.             this.propertyType = params[0];
  104.          }
  105.  
  106.       } catch (IntrospectionException var2) {
  107.          throw var2;
  108.       }
  109.    }
  110.  
  111.    public Class getPropertyEditorClass() {
  112.       return this.propertyEditorClass;
  113.    }
  114.  
  115.    public Class getPropertyType() {
  116.       return this.propertyType;
  117.    }
  118.  
  119.    public Method getReadMethod() {
  120.       return this.readMethod;
  121.    }
  122.  
  123.    public Method getWriteMethod() {
  124.       return this.writeMethod;
  125.    }
  126.  
  127.    public boolean isBound() {
  128.       return this.bound;
  129.    }
  130.  
  131.    public boolean isConstrained() {
  132.       return this.constrained;
  133.    }
  134.  
  135.    public void setBound(boolean bound) {
  136.       this.bound = bound;
  137.    }
  138.  
  139.    public void setConstrained(boolean constrained) {
  140.       this.constrained = constrained;
  141.    }
  142.  
  143.    public void setPropertyEditorClass(Class propertyEditorClass) {
  144.       this.propertyEditorClass = propertyEditorClass;
  145.    }
  146. }
  147.